extends Node

var currentScene = null #FIXME: name: currentWorldScene
var loaded_level = null
var w = null

func _ready():
	currentScene = get_tree().get_root().get_child(get_tree().get_root().get_child_count() - 1 )
	
#rename to "change_level"
func loadLevel(scene, level_scene, spawn, save):
	
	print ("global.gd::loadLevel called")
	loaded_level = level_scene

	if currentScene.has_method("clear_data"):
		currentScene.clear_data()
	currentScene.queue_free()
	currentScene.set_name("World_delete")
	w = load(scene)
	
	currentScene = w.instance() #world instance
	
	currentScene.set_name("World")
	
	
	get_tree().get_root().add_child(currentScene)
	
	currentScene.set_level(level_scene, spawn, save)
	
	print ("global.gd::loadLevel call finished")

	

func restartLevel():
	currentScene.reset_world()

func getCurrentWorld():
	return currentScene
	
#FIXME: crash if given scene path not found / scene not exists
func changeToCustomScene(scenePath):
	loaded_level = null
	currentScene.queue_free()
	
	var customScene = load(scenePath)
	
	currentScene = customScene.instance() 
	
	get_tree().get_root().add_child(currentScene)
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
